package com.crime.crimemonitorclient;

import org.xmlpull.v1.XmlPullParser;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.crimemonitor.library.CrimesSummary;

public abstract class CrimeChartViewFragment extends Fragment implements
		GoogleChartCallbackListener
{
	private TextView		mTitle;
	protected WebView		mWebView;
	private ProgressBar		mRenderingProgressBar;
	protected String		mContentTemplate;
	protected Float			mGraphHeightInDp;
	protected CrimesSummary	mCrimesSummary;

	public void init(String contentTemplate, float graphHeightInDp)
	{
		mContentTemplate = contentTemplate;
		mGraphHeightInDp = graphHeightInDp;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState)
	{
		View view = inflater.inflate(this.getFragmentLayoutId(), container,
				false);

		// web view setup
		mWebView = (WebView) view
				.findViewById(R.id.crime_chartview_fragment_webView);
		
		this.setupWebView();

		// progress bar
		mRenderingProgressBar = (ProgressBar) view
				.findViewById(R.id.crime_chartview_fragment_progressBar);

		// title
		mTitle = (TextView) view
				.findViewById(R.id.crime_chartview_fragment_title);
		mTitle.setText(this.getTitle());
		//mTitle.setVisibility(View.GONE);

		// set the visibility of the layout members
		// mTitle.setVisibility(View.GONE);
		// mWebView.setVisibility(View.GONE);
		mRenderingProgressBar.setVisibility(View.GONE);

		return view;
	}

	protected void setupWebView()
	{
		WebSettings settings = mWebView.getSettings();
		settings.setJavaScriptEnabled(true);
		settings.setUseWideViewPort(true);
		settings.setLoadWithOverviewMode(true);
		mWebView.addJavascriptInterface(new GoogleChartCallbackWebAppInterface(
				this), "CallbackListener");
	}

	protected int getFragmentLayoutId()
	{
		return R.layout.crime_chartview_fragment_layout;
	}

	public void load(CrimesSummary crimesSummary)
	{
		mCrimesSummary = crimesSummary;

		this.initBeforeCreatingChart();
		this.redraw();
	}

	protected void redraw()
	{
		String content = this.getContentFromTemplate();

		
		this.updateUIBeforeChartLoad();
		
		// nahraj do webview pripravenou stranku s definici grafu
		this.mWebView.loadDataWithBaseURL("file:///android_asset", content,
				"text/html", "utf-8", null);
	}

	protected abstract String getContentFromTemplate();

	protected abstract String getTitle();

	protected void initBeforeCreatingChart()
	{
	}

	protected void updateUIBeforeChartLoad()
	{
		mWebView.setVisibility(View.GONE);
		mRenderingProgressBar.setVisibility(View.VISIBLE);
	}

	protected void updateUIAfterChartLoad()
	{
		// po obdrzeni callbacku 'ready' od instance Google Chart se
		// schova progress bar a zobrazi normalni obsah
		mRenderingProgressBar.setVisibility(View.GONE);
		mTitle.setVisibility(View.VISIBLE);
		mWebView.setVisibility(View.VISIBLE);
	}
	
	@Override
	public void ready()
	{
		this.getView().post(new Runnable()
		{
			@Override
			public void run()
			{
				CrimeChartViewFragment.this.updateUIAfterChartLoad();
			}
		});
	}
}
